package com.itboye.pondteam.pondteam;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.pondteam.R;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.custom.ClearEditText;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.EmptyUtil;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import com.itboye.pondteam.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

import static com.itboye.pondteam.utils.EmptyUtil.getSp;

public class ForgetPasswordActivity extends BaseActivity implements Observer {

    ImageView img_back;
    TextView txt_title, btn_ok, btn_cancel, btn_get_yzm;
    ClearEditText edit_make_sure_password, edit_username, edit_password, edit_yzm;
    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        txt_title.setText(getString(R.string.forget_pass_title));
        userPresenter = new UserPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_ok:
                if (EmptyUtil.isEmpty(edit_username)) {
                    MAlert.alert(getString(R.string.user_empty));
                    return;
                }
                if (EmptyUtil.isEmpty(edit_yzm)) {
                    MAlert.alert(getString(R.string.yzm_empty));
                    return;
                }
                if (EmptyUtil.isEmpty(edit_password)) {
                    MAlert.alert(getString(R.string.pass_empty));
                    return;
                }
                if (!EmptyUtil.getCustomText(edit_password).equals(EmptyUtil.getCustomText(edit_make_sure_password))) {
                    MAlert.alert(getString(R.string.different_password));
                    return;
                }
                userPresenter.updatePass(getSp(Const.S_ID), EmptyUtil.getCustomText(edit_yzm), EmptyUtil.getCustomText(edit_username), EmptyUtil.getCustomText(edit_password));
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_get_yzm:
                if (EmptyUtil.isEmpty(edit_username)) {
                    MAlert.alert(getString(R.string.user_empty));
                    return;
                }
                userPresenter.sendEmailCode(EmptyUtil.getCustomText(edit_username),"2");
                break;
        }
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity resultEntity = handlerError(data);
        if (resultEntity != null) {
            if (resultEntity.getCode() != 0) {
                MAlert.alert(resultEntity.getMsg());
                return;
            }
            if (resultEntity.getEventType() == UserPresenter.update_pass_success) {
                MAlert.alert(resultEntity.getData());
                finish();
            } else if (resultEntity.getEventType() == UserPresenter.update_pass_fail) {
                MAlert.alert(resultEntity.getData());
            }else if (resultEntity.getEventType() == UserPresenter.sendEmailCodesuccess) {
                MAlert.alert(resultEntity.getData());
            }else if (resultEntity.getEventType() == UserPresenter.sendEmailCodesuccess) {
                MAlert.alert(resultEntity.getData());
            }
        }
    }
}
